package com.cljh.service.impl;


import com.cljh.bean.Path;
import com.cljh.bean.Role;
import com.cljh.bean.RoleMenu;
import com.cljh.controller.page.PageBean;
import com.cljh.mapper.RoleMapper;
import com.cljh.mapper.RoleMenuMapper;
import com.cljh.service.MenuService;
import com.cljh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Override
    public boolean save(Role role) {
        return roleMapper.save(role) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return roleMapper.delete(id) > 0;
    }

    @Override
    public boolean Upd(Role role) {
        return roleMapper.Upd(role) > 0;
    }

    @Override
    public Role selectById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public PageBean<Role> selectAll(Integer current, Integer size, Role role) {
        int begin = (current - 1) * size;
        String name = role.getName();
        String description = role.getDescription();
        if (name != null && name.length() > 0) {
            role.setName("%" + name + "%");
        }
        if (description != null && description.length() > 0) {
            role.setDescription("%" + description + "%");
        }
        List<Role> roles = roleMapper.selectAll(begin, size, role);
        int i = roleMapper.selectCount(role);
        PageBean<Role> rolePageBean = new PageBean<>();
        rolePageBean.setRows(roles);
        rolePageBean.setTotalCount(i);

        return rolePageBean;
    }

    @Override
    public boolean deleteIds(List<Integer> ids) {

        return roleMapper.deleteIds(ids) > 0;
    }

    @Autowired
    RoleMenuMapper roleMenuMapper;
    @Autowired
    MenuService menuService;

    @Transactional
    @Override
    public void setRoleMenu(Integer roleId, List<Integer> menuIds) {
        roleMenuMapper.delete(roleId);
        for (Integer id : menuIds) {
            Path path = menuService.selectById(id);
            //判断是否是二级菜单
            if (path.getPathId() != null && !menuIds.contains(path.getPathId())) {
                //添加父级id
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(path.getPathId());
                roleMenuMapper.save(roleMenu);
            }
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(id);
            roleMenuMapper.save(roleMenu);
        }
    }

    @Override
    public List<Integer> getRoleMenu(Integer roleId) {
        List<Integer> roleMenus = roleMenuMapper.selectByRoleId(roleId);
        return roleMenus;
    }

    @Override
    public List<Role> selectAll1() {
        return roleMapper.selectAll1();
    }
}
